//
//  RegisterViewController.m
//  SuperMedical
//
//  Created by 赵隆杰 on 2018/7/28.
//  Copyright © 2018年 赵隆杰. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterApi.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (weak, nonatomic) IBOutlet UIImageView *moblieImage;
@property (weak, nonatomic) IBOutlet UITextField *moblieField;
@property (weak, nonatomic) IBOutlet UIImageView *passwordImage;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIView *forgetView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *confirmField;

@end

@implementation RegisterViewController

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self configView];
    
    UILabel *argeeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, SL_WINDOW_HEIGHT - UIDevice.vg_safeDistanceBottom - 100, 90, 40)];
    argeeLabel.font = [UIFont systemFontOfSize:12];
    argeeLabel.text = @"注册即代表同意";
    argeeLabel.textColor = kColor333333;
    [self.view addSubview:argeeLabel];
    
    UIButton *argeeBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(argeeLabel.frame), argeeLabel.frame.origin.y, 80, 40)];
    argeeBtn1.tag = 1;
    argeeBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
    [argeeBtn1 setTitle:@"《隐私协议》" forState:UIControlStateNormal];
    if (@available(iOS 13.0, *)) {
        [argeeBtn1 setTitleColor:[UIColor linkColor] forState:UIControlStateNormal];
    } else {
        [argeeBtn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    [argeeBtn1 addTarget:self action:@selector(argeeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:argeeBtn1];
    
    UIButton *argeeBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(argeeBtn1.frame), argeeLabel.frame.origin.y, 80, 40)];
    argeeBtn2.tag = 2;
    argeeBtn2.titleLabel.font = [UIFont systemFontOfSize:12];
    [argeeBtn2 setTitle:@"《用户协议》" forState:UIControlStateNormal];
    if (@available(iOS 13.0, *)) {
        [argeeBtn2 setTitleColor:[UIColor linkColor] forState:UIControlStateNormal];
    } else {
        [argeeBtn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    [argeeBtn2 addTarget:self action:@selector(argeeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:argeeBtn2];
}

- (void)configView
{
    self.loginBtn.layer.cornerRadius = self.loginBtn.height/2;
    self.loginBtn.layer.masksToBounds = YES;
}

- (IBAction)loginClick:(id)sender {
    NSLog(@"注册");
    
    [self.view endEditing:YES];
    if ([self.moblieField.text isEqualToString:@""]) {
        [self.view makeToast:@"请设置手机号" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    if (![self.passwordField.text isEqualToString:self.confirmField.text]) {
        [self.view makeToast:@"两次密码不一致" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    //登录接口不传token和uid
    RegisterApi *api = [[RegisterApi alloc] initWithUserName:self.moblieField.text passWord:self.passwordField.text];
    api.hideHUD = YES;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"注册中...";
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [hud hideAnimated:YES];
        if (api.success) {
            VHWeakSelf
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"注册成功，请返回重新登录" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                VHStrongSelf
                [self.navigationController popViewControllerAnimated:YES];
            }]];
            [[UIWindow currentViewController] presentViewController:alertController animated:YES completion:nil];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [hud hideAnimated:YES];
    }];
}

- (IBAction)hasAccountClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)argeeBtnClick:(UIButton *)button
{
    if (button.tag == 1) {
        //隐私协议
        [[AppLogic sharedInstance] pushToWebViewVC:@"隐私协议" url:@"https://www.7pa.com/yhhnqply.html"];
    } else {
        //用户协议
        [[AppLogic sharedInstance] pushToWebViewVC:@"用户协议" url:@"https://www.7pa.com/yhhnqply.html"];
    }
}

@end
